package com.situ.blog.controller;


import com.situ.blog.pojo.entity.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.pojo.vo.BlogVo;
import com.situ.blog.service.IBlogService;
import com.situ.blog.service.impl.BlogServiceImpl;
import com.situ.blog.util.MyBeanUtils;
import com.situ.blog.util.PageInfo;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/blog")
public class BlogController extends BaseController {
    private IBlogService blogService= new BlogServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BlogServlet.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case  "updateStatus":
                updateStatus(req, resp);
                break;
        }
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        blogService.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        toJSON(resp, Result.ok("更新成功！"));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.update");
        Blog blog = MyBeanUtils.copyToBean(req, Blog.class);
        blogService.update(blog);
        toJSON(resp, Result.ok("修改成功！"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.selectById");
        String id = req.getParameter("id");
        Blog blog = blogService.selectById(Integer.parseInt(id));
        toJSON(resp, Result.ok(blog));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        blogService.deleteAll(ids);
        toJSON(resp, Result.ok("删除成功！"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.delete");
        String id = req.getParameter("id");
        blogService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功！"));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.add");
        Blog blog = MyBeanUtils.copyToBean(req, Blog.class);
        blogService.add(blog);
        toJSON(resp, Result.ok("添加成功！"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.selectByPage");
        BlogQuery blogQuery = MyBeanUtils.copyToBean(req, BlogQuery.class);
        PageInfo pageInfo = blogService.selectByPage(blogQuery);
        toJSON(resp, pageInfo);
    }
}
